/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package jim.h.commons;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

import com.taobao.tddl.client.jdbc.TDataSource;

/**
 * @author jinggang.huangjg 2013-1-8 下午7:46:39
 */
public class YugongCheckTest {

    private static String sourceRuleString = "<?xml version=\"1.0\" encoding=\"gb2312\"?>"
                                             + "<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">"
                                             + "<beans>"
                                             + "<bean id=\"vtabroot\" class=\"com.taobao.tddl.interact.rule.VirtualTableRoot\" init-method=\"init\">"
                                             + "<property name=\"dbType\" value=\"MYSQL\" />"
                                             + "<property name=\"tableRules\">"
                                             + "<map>"
                                             + "<entry key=\"delivery_template\" value-ref=\"deliveryTemplateTableRule\" />"
                                             + "<entry key=\"delivery_sub_template\" value-ref=\"deliverySubTemplateTableRule\" />"
                                             + "<entry key=\"delivery_rate\" value-ref=\"deliveryRateTableRule\" />"
                                             + "<entry key=\"delivery_template_mapping\" value-ref=\"deliveryTemplateMappingTableRule\" />"
                                             + "<entry key=\"delivery_template_migration\" value-ref=\"deliveryTemplateMigrationTableRule\" />"
                                             + "</map>"
                                             + "</property>"
                                             + "</bean>"
                                             + "<bean id=\"deliveryTemplateTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB{0}_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_TEMPLATE_{0000}\" />"
                                             + "<property name=\"dbRules\" value=\"(Math.abs(#member_id,1,128#.hashCode()) % 128).intdiv(16) + 1\" />"
                                             + "<property name=\"tbRules\" value=\"Math.abs(#member_id,1,128#.hashCode()) % 128\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />"
                                             + "</bean>"
                                             + "<bean id=\"deliverySubTemplateTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB{0}_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_SUB_TEMPLATE_{0000}\" />"
                                             + "<property name=\"dbRules\" value=\"(Math.abs(#member_id,1,128#.hashCode()) % 128).intdiv(16) + 1\" />"
                                             + "<property name=\"tbRules\" value=\"Math.abs(#member_id,1,128#.hashCode()) % 128\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />"
                                             + "</bean>"
                                             + "<bean id=\"deliveryRateTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB{0}_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_RATE_{0000}\" />"
                                             + "<property name=\"dbRules\" value=\"(Math.abs(#member_id,1,128#.hashCode()) % 128).intdiv(16) + 1\" />"
                                             + "<property name=\"tbRules\" value=\"Math.abs(#member_id,1,128#.hashCode()) % 128\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />"
                                             + "</bean>"
                                             + "<bean id=\"deliveryTemplateMappingTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB{0}_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_TEMPLATE_MAPPING_{0000}\" />"
                                             + "<property name=\"dbRules\" value=\"(Math.abs(#member_id,1,128#.hashCode()) % 128).intdiv(16) + 1\" />"
                                             + "<property name=\"tbRules\" value=\"Math.abs(#member_id,1,128#.hashCode()) % 128\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />"
                                             + "</bean>"
                                             + "<bean id=\"deliveryTemplateMigrationTableRule\" class=\"com.taobao.tddl.interact.rule.TableRule\">"
                                             + "<property name=\"dbNamePattern\" value=\"ITU_DB_GROUP\" />"
                                             + "<property name=\"tbNamePattern\" value=\"DELIVERY_TEMPLATE_MIGRATION\" />"
                                             + "<property name=\"allowFullTableScan\" value=\"true\" />" + "</bean>"
                                             + "</beans>";

    public static void main(String[] args) throws SQLException {
        DataSource dataSource = createDataSource();
        DataSource srcDtaSource = createSrcDataSource();
        Connection srcConn = null;
        Connection conn = null;
        PreparedStatement srcPstm = null;
        PreparedStatement pstm = null;
        ResultSet srcRs = null;
        ResultSet rs = null;
        try {
            conn = dataSource.getConnection();
            srcConn = srcDtaSource.getConnection();

            pstm = conn.prepareStatement("select count(*) c from delivery_template where id < 2000000");
            rs = pstm.executeQuery();
            rs.next();
            System.out.println("delivery_template records = " + rs.getInt("c"));

            pstm = conn.prepareStatement("select count(*) c from delivery_sub_template where id < 2000000");
            rs = pstm.executeQuery();
            rs.next();
            System.out.println("delivery_sub_template records = " + rs.getInt("c"));

            pstm = conn.prepareStatement("select count(*) c from delivery_rate where id < 2000000");
            rs = pstm.executeQuery();
            rs.next();
            System.out.println("delivery_rate records = " + rs.getInt("c"));

            srcPstm = srcConn.prepareStatement("select count(*) c from freight_template");
            srcRs = srcPstm.executeQuery();
            srcRs.next();
            System.out.println("freight_template records = " + rs.getInt("c"));

            srcPstm = srcConn.prepareStatement("select count(*) c from weight_tariff");
            srcRs = srcPstm.executeQuery();
            srcRs.next();
            System.out.println("weight_tariff records = " + rs.getInt("c"));
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstm != null) {
                    pstm.close();
                }
                if (conn != null) {
                    conn.close();
                }
                if (srcPstm != null) {
                    srcPstm.close();
                }
                if (srcRs != null) {
                    srcRs.close();
                }
                if (srcPstm != null) {
                    srcPstm.close();
                }
                if (srcConn != null) {
                    srcConn.close();
                }
            } catch (SQLException e) {
            }
        }
    }

    private static DataSource createDataSource() {
        TDataSource ds = new TDataSource();
        ds.setAppName("ITUWL_APP");
        ds.setAppRuleString(sourceRuleString);
        ds.init();
        return ds;
    }

    private static DataSource createSrcDataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        ds.setUsername("alibaba");
        ds.setPassword("ccbutest");
        ds.setUrl("jdbc:oracle:thin:@10.20.36.19:1521:ocntest");
        ds.setInitialSize(1);
        return ds;
    }
}
